package com.yunkaweilai.android.activity.lanya;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.lazylibrary.util.PreferencesUtils;
import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.utils.TitleBuilder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 蓝牙设置
 * Created by Administrator on 2017/10/18.
 */

public class LanyaSetting extends BaseActivity {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_listview)
    ListView idListview;
    @BindView(R.id.id_tv_all_member)
    TextView idTvAllMember;
    @BindView(R.id.id_checkbox)
    CheckBox idCheckbox;
    @BindView(R.id.id_rb1)
    RadioButton idRb1;
    @BindView(R.id.id_rb2)
    RadioButton idRb2;
    @BindView(R.id.id_rgp)
    RadioGroup idRgp;
    private int selectPosition = -1;//用于记录用户选择的变量
    private ArrayList<BluetoothDevice> bluetoothLists = new ArrayList<>();
    private MyAdapter adapter;
    private BluetoothDevice device = null;
    public static final String BLUETOOTH_ADDRESS = "BLUETOOTH_ADDRESS";
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                bluetoothLists.clear();
                bluetoothLists.addAll(BluetoothAdapter.getDefaultAdapter().getBondedDevices());
                adapter.notifyDataSetChanged();
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lanya_setting);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("打印设置")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setRightText("确定")
                .setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (device == null) {
                            finish();
                        } else {
                            PreferencesUtils.putString(mAc, BLUETOOTH_ADDRESS, device.getAddress());
                            finish();
                            showToast("选择成功");
                        }

                    }
                });
        boolean aBoolean1 = PreferencesUtils.getBoolean(mAc, CommonConstants.LANYA_DAYING_58OR80, false);
        if (aBoolean1){
            idRb2.setChecked(true);
        }else{
            idRb1.setChecked(true);
        }

        boolean aBoolean = PreferencesUtils.getBoolean(mAc, CommonConstants.LANYA_DAYING_ZIDONG, false);
        idCheckbox.setChecked(aBoolean);
        initView();
        initClick();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(receiver, intentFilter);
    }

    private void initView() {
        bluetoothLists.clear();
        bluetoothLists.addAll(BluetoothAdapter.getDefaultAdapter().getBondedDevices());
        String address = PreferencesUtils.getString(mAc, BLUETOOTH_ADDRESS);
        if (!StringUtils.isEmpty(address)) {
            for (int i = 0; i < bluetoothLists.size(); i++) {
                BluetoothDevice bluetoothDevice = bluetoothLists.get(i);
                if (bluetoothDevice.getAddress().equals(address)) {
                    selectPosition = i;
                    device = bluetoothLists.get(i);
                }
            }
        }
        adapter = new MyAdapter(this, bluetoothLists);
        idListview.setAdapter(adapter);

    }

    private void initClick() {
        idListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取选中的参数
                selectPosition = position;
                adapter.notifyDataSetChanged();
                device = bluetoothLists.get(position);
                showLog(device.getAddress() + ":" + device.getName());
            }
        });

        idCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PreferencesUtils.putBoolean(mAc, CommonConstants.LANYA_DAYING_ZIDONG, isChecked);
            }
        });

        idRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.id_rb1:
                        PreferencesUtils.putBoolean(mAc, CommonConstants.LANYA_DAYING_58OR80, false);
                        break;
                    case R.id.id_rb2:
                        PreferencesUtils.putBoolean(mAc, CommonConstants.LANYA_DAYING_58OR80, true);
                        break;
                }
            }
        });
    }


    public class MyAdapter extends BaseAdapter {
        Context context;
        ArrayList<BluetoothDevice> bluetoothList;
        LayoutInflater mInflater;

        public MyAdapter(Context context, ArrayList<BluetoothDevice> mList) {
            this.context = context;
            this.bluetoothList = mList;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return bluetoothList.size();
        }

        @Override
        public BluetoothDevice getItem(int position) {
            return bluetoothList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_list_lanya, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView) convertView.findViewById(R.id.id_name);
                viewHolder.select = (RadioButton) convertView.findViewById(R.id.id_select);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.name.setText(getItem(position).getName());
            if (selectPosition == position) {
                viewHolder.select.setChecked(true);
            } else {
                viewHolder.select.setChecked(false);
            }
            return convertView;
        }
    }

    public class ViewHolder {
        TextView name;
        RadioButton select;
    }

}
